package edu.sctu;


import edu.sctu.DbUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * UserDao
 * 用于User的数据访问
 */
public class UserDao {

    DbUtil dbUtil = new DbUtil();

    /**
     * 登录验证
     * @param connection
     * @param user
     * @return
     * @throws Exception
     */
    public User login(Connection connection,User user)throws Exception{
        User resultUser = null;//查询结果User
        String sql="select * from t_users where name=? and password=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);

        //设置参数
        preparedStatement.setString(1,user.getName());
        preparedStatement.setString(2,user.getPassword());
        //执行
        ResultSet resultSet=preparedStatement.executeQuery();

        if (resultSet.next()){
            resultUser = new User();
            resultUser.setId(resultSet.getInt("id"));
            resultUser.setName(resultSet.getString("name"));
            resultUser.setPassword(resultSet.getString("password"));

        }
        return resultUser;
    }

    /**
     * 注册或添加用户
     * @param connection
     * @param user
     * @throws SQLException
     */
    public void register(Connection connection,User user) throws SQLException {

        String sql = "INSERT INTO t_users(name,password) VALUES (?,?)";

        //预编译
        PreparedStatement preparedStatement = connection.prepareStatement(sql); //预编译SQL，减少sql执行


        //设置参数
        preparedStatement.setString(1,user.getName());
        preparedStatement.setString(2,user.getPassword());

        //执行
        preparedStatement.execute();
    }

    /**
     * 判断用户是否存在
     * @param connection
     * @param user
     */

    public boolean isExist(Connection connection,User user) throws SQLException {

        User resultUser = null;//查询结果User
        String sql="select * from t_users where name=?";
        PreparedStatement preparedStatement=connection.prepareStatement(sql);

        //设置参数(通过用户输入的用户名查询)
        preparedStatement.setString(1,user.getName());

        //执行
        ResultSet resultSet=preparedStatement.executeQuery();

        if (resultSet.next()){
            return false;
        }else {
            return true;
        }

    }

    /**
     * 用户更新
     * @param connection
     * @param user
     * @throws SQLException
     */
    public void update(Connection connection,User user) throws SQLException {
        String sql = "UPDATE t_users SET name =?,password=? WHERE id=?";

        //预编译
        PreparedStatement preparedStatement = connection.prepareStatement(sql); //预编译SQL，减少sql执行

        //传参
        System.out.println("用户名更新结果："+user.getName());
        preparedStatement.setString(1, user.getName());
        preparedStatement.setString(2, user.getPassword());
        preparedStatement.setString(3, String.valueOf(user.getId()));

        //执行
        preparedStatement.execute();
    }

    /**
     * 删除
     * @param connection
     * @param user
     * @throws SQLException
     */
    public void delete(Connection connection,User user) throws SQLException {

        String sql = "delete from t_users where id=?";

        //预编译SQL，减少sql执行
        PreparedStatement ptmt = connection.prepareStatement(sql);

        //传参
        ptmt.setInt(1, Integer.parseInt(String.valueOf(user.getId())));

        //执行
        ptmt.execute();
    }

    /**
     * 用户列表
     * @return
     */
    public List<User> getUserList(){
        List<User> userList = new ArrayList<User>();

        Connection connection = null;
        Statement statement = null;
        ResultSet resultSet = null;


        try {
            String sql = "select * from t_users";
            connection = dbUtil.getCon();
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);

            //遍历数据集
            while (resultSet.next()) {
                int id = resultSet.getInt(1);
                String name = resultSet.getString(2);
                String password = resultSet.getString(3);

                //封装在User中
                User user = new User();
                user.setId(id);
                user.setName(name);
                user.setPassword(password);

                userList.add(user);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //关闭
            try {
                resultSet.close();
                statement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }

        return userList;
    }
}